package com.jtmg.glorykingsnews.service.impl;

import com.github.pagehelper.PageException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jtmg.glorykingsnews.domain.TbUser;
import com.jtmg.glorykingsnews.mapper.TbUserMapper;
import com.jtmg.glorykingsnews.service.TbUserService;
import com.jtmg.glorykingsnews.vo.commom.LayuiReturnVo;
import com.jtmg.glorykingsnews.vo.commom.MsgVo;
import com.jtmg.glorykingsnews.vo.user.UserQuery;
import com.jtmg.glorykingsnews.vo.user.UserVo;
import com.jtmg.glorykingsnews.vo.user.UserVoForQuery;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class TbUserServiceImpl implements TbUserService {

    @Resource
    private TbUserMapper tbUserMapper;


    Date date = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String currentTime = formatter.format(date);


    @Override
    public MsgVo<TbUser> login(UserVo vo) {
        Example example = new Example(TbUser.class);
        example.createCriteria().andEqualTo("username", vo.getUserName());
        TbUser user = tbUserMapper.selectOneByExample(example);
        MsgVo<TbUser> msgVo = new MsgVo<>();
        if (user != null && DigestUtils.md5DigestAsHex(vo.getPassword().getBytes()).equals(user.getPassword())) {
            if (user.getDeleted() == 1) {
                msgVo.setCode("201");
                msgVo.setMsg("该用户已被注销");
            } else {
                msgVo.setCode("200");
                msgVo.setMsg("登陆成功");
                msgVo.setObj(user);
            }

        } else {
            msgVo.setCode("201");
            msgVo.setMsg("用户名或密码错误，请重新输入");
        }
        return msgVo;
    }

    @Override
    public LayuiReturnVo<TbUser> register(UserVo vo) {

        Example example = new Example(TbUser.class);
        example.createCriteria().andEqualTo("username",vo.getUserName())
                .andEqualTo("deleted","0");
        List<TbUser> users = tbUserMapper.selectByExample(example);
        if (users.size() > 0){
            return new LayuiReturnVo<>(0,"该用户已存在");
        }
        TbUser user = new TbUser();
        user.setId(UUID.randomUUID().toString());
        user.setUsername(vo.getUserName());
        user.setPassword(DigestUtils.md5DigestAsHex(vo.getPassword().getBytes()));
        user.setSalt("0");
        user.setRealName(vo.getRealName());
        user.setPhone(vo.getPhone());
        user.setCreateTime(new Date());
        user.setModifyTime(new Date());
        user.setDeleted(0);
        tbUserMapper.insert(user);
        return new LayuiReturnVo(0, "新增成功");
    }

//    @Override
//    public MsgVo<TbUser> register(UserVo vo) {
//        MsgVo<TbUser> msgVo = new MsgVo<>();
//
//        Example example = new Example(TbUser.class);
//        example.createCriteria().andEqualTo("username", vo.getUserName());
////        String username = tbUserMapper.selectOneByExample(example).getUsername();
//        List<TbUser> users1 = tbUserMapper.selectByExample(example);
//        System.out.println(users1.size());
//        Example example1 = new Example(TbUser.class);
//        example1.createCriteria().andEqualTo("phone", vo.getPhone());
////        String phone = tbUserMapper.selectOneByExample(example1).getPhone();
//        List<TbUser> users2 = tbUserMapper.selectByExample(example1);
//        //根据用户名查询是否有重名的用户
//        if(users1.size() !=0 ){
//            System.out.println("users1："+users1.get(0).toString());
//            msgVo.setCode("201");
//            msgVo.setMsg("该用户已被注册");
//            //根据电话号码查询是否有重复的电话号码
//        } else if(users1.size() !=0 ){
//            msgVo.setCode("201");
//            msgVo.setMsg("该号码已被注册");
//        }else {
//            //注册到数据库
//            try{
//                TbUser user = new TbUser();
//                user.setId(UUID.randomUUID().toString());
//                System.out.println(user.getId());
//                user.setUsername(vo.getUserName());
//                System.out.println(user.getUsername());
//                user.setPassword(DigestUtils.md5DigestAsHex(vo.getPassword().getBytes()));
//                System.out.println(user.getPassword());
//                user.setRealName(vo.getRealName());
//                System.out.println(user.getRealName());
//                user.setPhone(vo.getPhone());
//                System.out.println(user.getPhone());
//                user.setCreateTime(new Date());
//                System.out.println(user.getCreateTime());
//                user.setModifyTime(new Date());
//                System.out.println(user.getModifyTime());
//                user.setDeleted(0);
//                System.out.println("11111111");
//                int resault = tbUserMapper.insert(user);
//                System.out.println(resault);
//                msgVo.setCode("200");
//                msgVo.setMsg("注册成功");
//            }catch (Exception e){
//                msgVo.setCode("201");
//                msgVo.setMsg("注册异常");
//            }
//        }
//
//        return msgVo;
//    }

    @Override
    public LayuiReturnVo<UserVoForQuery> pageQuery(UserQuery query) throws ParseException {
        Example example = new Example(TbUser.class);
        Example.Criteria criteria = example.createCriteria().andEqualTo("deleted", 0);
        String name = query.getUserName();
        String phone = query.getPhone();
        //如果name是空，那么就展示所有用户信息
        if (null!=name&&!"".equals(name)){
            criteria.andLike("username","%"+name+"%");
        }
        if (null!=phone&&!"".equals(phone)){
            criteria.andLike("phone","%"+phone+"%");
        }
        int count = tbUserMapper.selectCountByExample(example);
//        PageHelper.startPage(query.getPage(), query.getLimit());
        List<TbUser> userList = tbUserMapper.selectByExample(example);
        List<UserVoForQuery> userVoForQueries = new ArrayList<>();
        for (TbUser user : userList){
            UserVoForQuery userVoForQuery = new UserVoForQuery();
            userVoForQuery.setId(user.getId());
            userVoForQuery.setUsername(user.getUsername());
            userVoForQuery.setPassword(user.getPassword());
            userVoForQuery.setSalt(user.getSalt());
            userVoForQuery.setPhone(user.getPhone());
            userVoForQuery.setRealName(user.getRealName());
            userVoForQuery.setDeleted(user.getDeleted());
            userVoForQuery.setCreateTime(formatter.format(user.getCreateTime()));
            userVoForQuery.setModifyTime(formatter.format(user.getModifyTime()));
            userVoForQueries.add(userVoForQuery);
        }
        return new LayuiReturnVo<UserVoForQuery>(0,"查询用户信息成功",count,userVoForQueries);
    }

    @Override
    public MsgVo update(TbUser vo) {
        System.out.println("进入修改的后台"+":"+vo.getId()+vo.getRealName()+vo.getPhone());
        try {
            System.out.println(vo.getId());
            TbUser User = tbUserMapper.selectByPrimaryKey(vo.getId());
            if (null == User) {
                return new MsgVo("201", "该用户不存在");
            }
            vo.setModifyTime(new Date());
            System.out.println(vo);
            tbUserMapper.updateByPrimaryKeySelective(vo);
            return new MsgVo("200", "修改成功");
        } catch (Exception e) {
            System.out.println(e.toString());
            return new MsgVo("201", "修改失败");
        }
    }

    @Override
    public MsgVo delete(String id) {
        try {
            System.out.println(id);
            TbUser user = tbUserMapper.selectByPrimaryKey(id);
            if (null == user) {
                return new MsgVo("201", "该用户不存在");
            }
            user.setDeleted(1);
            user.setModifyTime(new Date());
            tbUserMapper.updateByPrimaryKey(user);
            return new MsgVo("200", "删除成功");
        } catch (Exception e) {
            System.out.println(e.toString());
            return new MsgVo("201", "删除失败");
        }
    }


}
